package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.po.DishPO;
import com.sky.po.SetmealPO;
import com.sky.repository.CategoryRepository;
import com.sky.repository.DishRepository;
import com.sky.po.CategoryPO;
import com.sky.repository.SetmealRepository;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private DishRepository dishRepository;
    @Autowired
    private SetmealRepository setmealRepository;

    /**
     * 新增分类
     * @param categoryDTO
     */
    public Boolean save(CategoryDTO categoryDTO) {
        CategoryPO category = CategoryMapper.INSTANCE.DTOConvertPO(categoryDTO);
        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        return categoryRepository.insert(category)>0;
    }

    /**
     * 分页查询
     * @param source
     * @return
     */
    public PageResult pageQuery(CategoryPageQueryDTO source) {
        Page<CategoryPO> page = new Page<>(source.getPage(),source.getPageSize());
        categoryRepository.pageQuery(page, source);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    /**
     * 根据id删除分类
     * @param id
     */
    public void deleteById(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        int count = dishRepository.selectCount(new LambdaQueryWrapper<DishPO>()
                .eq(DishPO::getCategoryId,id)).intValue();
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealRepository.selectCount(new LambdaQueryWrapper<SetmealPO>()
                .eq(SetmealPO::getCategoryId,id)).intValue();
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        categoryRepository.deleteById(id);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    public void update(CategoryDTO categoryDTO) {
        categoryRepository.updateById(CategoryMapper.INSTANCE.DTOConvertPO(categoryDTO));
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        CategoryPO categoryPO = categoryRepository.selectById(id);
        categoryPO.setStatus(status>0?1:0);
        categoryRepository.updateById(categoryPO);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    public List<CategoryPO> list(Integer type) {
        return categoryRepository.selectByType(type);
    }
}
